package cn.zjweu.service.impl;

import cn.zjweu.dao.CartDao;
import cn.zjweu.dao.DishDao;
import cn.zjweu.dao.OrderDao;
import cn.zjweu.dao.impl.CartDaoImpl;
import cn.zjweu.dao.impl.DishDaoImpl;
import cn.zjweu.dao.impl.OrderDaoImpl;
import cn.zjweu.domain.*;
import cn.zjweu.service.OrderService;

import java.util.List;

public class OrderServiceImpl implements OrderService {

    private OrderDao orderDao = new OrderDaoImpl();
    private CartDao cartDao = new CartDaoImpl();
    private DishDao dishDao = new DishDaoImpl();

    @Override
    public boolean saveOrder(int userId, Order order, String cartList) {
        try {
            int pKey = orderDao.saveOrder(order);
            System.out.println("添加后的主键:" + pKey);
            String delCart = cartList.substring(0, cartList.length() - 1);
            System.out.println("要从购物车中删除的" + delCart);

            for (String dishId : delCart.split(",")) {
                List<Dish> dish = dishDao.findCartListByDishIdAndUserId(userId, Integer.parseInt(dishId));
                System.out.println("查找成功！");
                for (Dish d : dish) {
                    if (orderDao.saveOrderDetails(pKey, d)) {
                        System.out.println("添加成功");
                    } else {
                        System.out.println("添加失败");
                        return MessageInfo.FAIL;
                    }
                }
            }

            if (cartDao.delByUserIdAndDList(order.getUser().getUserId(), delCart)) {
                System.out.println("删除成功！");
            } else {
                System.out.println("删除失败！");
                return MessageInfo.FAIL;
            }
        } catch (Exception e) {
            return MessageInfo.FAIL;
        }
        return MessageInfo.SUCCESS;
    }

    @Override
    public PageBean<Order> findOrderListByPage(int currentPage, int pageSize, String username) {
        PageBean<Order> orderPageBean = new PageBean<>();
        orderPageBean.setCurrentPage(currentPage);
        orderPageBean.setPageSize(pageSize);
        int totalCount = 0;
        List<Order> orderList;
        int start = (currentPage - 1) * pageSize;
        if (!"null".equals(username) && username.length() > 0) {
            totalCount = orderDao.findCountByUsername(username);
            orderList = orderDao.findOrderListByUsername(start, pageSize, username);
        } else {
            totalCount = orderDao.findCount();
            orderList = orderDao.findOrderList(start, pageSize);
        }
        orderPageBean.setTotalCount(totalCount);

        System.out.println("查询到" + totalCount + "条订单信息");

        int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        orderPageBean.setTotalPage(totalPage);
        for (Order order : orderList) {
            List<OrderDetails> orderDetailsList = orderDao.findOrderDetailsByOrderId(order.getOrderId());
            order.setOrderDetailsList(orderDetailsList);
        }
        orderPageBean.setList(orderList);
        return orderPageBean;
    }

    @Override
    public boolean changeOrderStatusByOrderId(int orderId, int status) {
        return orderDao.changeOrderStatusByOrderId(orderId, status);
    }
}
